package com.orhon.smartcampus.modules.teacher.fetcher;

import com.orhon.smartcampus.auth.JwtUtils;
import com.orhon.smartcampus.basicComponents.Result;
import com.orhon.smartcampus.modules.base.gqlModel.GQLPageParam;
import com.orhon.smartcampus.modules.base.mm.EnvArgsMapper;
import com.orhon.smartcampus.modules.material.entity.Buildings;
import com.orhon.smartcampus.modules.material.entity.Floor;
import com.orhon.smartcampus.modules.material.entity.Room;
import com.orhon.smartcampus.modules.material.entity.Unit;
import com.orhon.smartcampus.modules.material.gqlModel.GQLBuilding;
import com.orhon.smartcampus.modules.material.gqlModel.GQLFloor;
import com.orhon.smartcampus.modules.material.gqlModel.GQLRoom;
import com.orhon.smartcampus.modules.material.gqlModel.GQLUnit;
import com.orhon.smartcampus.modules.material.mm.BuildingMapper;
import com.orhon.smartcampus.modules.material.mm.FloorMapper;
import com.orhon.smartcampus.modules.material.mm.RoomMapper;
import com.orhon.smartcampus.modules.material.mm.UnitMapper;
import com.orhon.smartcampus.modules.student.entity.Arrives;
import com.orhon.smartcampus.modules.student.gqlModel.GQLArrives;
import com.orhon.smartcampus.modules.student.gqlModel.GQLStudentLearnInfo;
import com.orhon.smartcampus.modules.student.mm.ArrivesMapper;
import com.orhon.smartcampus.modules.systemctl.entity.OrgDepartments;
import com.orhon.smartcampus.modules.systemctl.gqlModel.GQLDepartment;
import com.orhon.smartcampus.modules.systemctl.mm.DepartmentMapper;
import com.orhon.smartcampus.modules.teacher.dao.AwardDao;
import com.orhon.smartcampus.modules.teacher.dao.OfficeArrangeDao;
import com.orhon.smartcampus.modules.teacher.entity.Award;
import com.orhon.smartcampus.modules.teacher.entity.OfficeArrange;
import com.orhon.smartcampus.modules.teacher.entity.TInformation;
import com.orhon.smartcampus.modules.teacher.gqlModel.GQLAward;
import com.orhon.smartcampus.modules.teacher.gqlModel.GQLTeacher;
import com.orhon.smartcampus.modules.teacher.gqlModel.GQLTeacherOffice;
import com.orhon.smartcampus.modules.teacher.mm.AwardMapper;
import com.orhon.smartcampus.modules.teacher.mm.TeacherMapper;
import com.orhon.smartcampus.modules.teacher.mm.TeacherOfficeMapper;
import com.orhon.smartcampus.modules.user.dao.TokenService;
import com.orhon.smartcampus.utils.R;
import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class TeacherOfficesFetcher implements DataFetcher {
	
	@Autowired
	OfficeArrangeDao officeArrangeDao;

	@Autowired
	TokenService tokenService;

	@Override
	public Object get(DataFetchingEnvironment env) throws Exception {

		String token = tokenService.getSystemToken();
		if(token==null) {
			return  R.NoLogin();
		}
		Long schoolId = JwtUtils.getSchoolId(token);

		List<GQLTeacherOffice> GQLTeacherOffice = new ArrayList<>();
		GQLPageParam page = EnvArgsMapper.MAPPER.fromArgs(env);
		String hql = "from OfficeArrange where deleted_at = null and school_id = " + schoolId;

		List<OfficeArrange> list = officeArrangeDao.find(hql,null,page.getPage(),page.getLimit());

		for(OfficeArrange item:list){
			GQLBuilding gqlbuildings = null;
			if (env.getSelectionSet().contains("data/buildings")){
				Buildings buildings = item.getMaterialBuildingsByBuildingsId();
				gqlbuildings = BuildingMapper.MAPPER.toBuilding(buildings);
			}

			GQLUnit  gqlUnit = null;
			if(env.getSelectionSet().contains("data/unit")){
				Unit unit = item.getMaterialUnitByUnitId();
				gqlUnit = UnitMapper.MAPPER.toUnit(unit);
			}

			GQLFloor gqlFloor = null;
			if(env.getSelectionSet().contains("data/floor")){

				Floor floor = item.getMaterialFloorByFloorId();
				gqlFloor = FloorMapper.MAPPER.toFloor(floor);
			}

			GQLRoom gqlRoom = null;
			if(env.getSelectionSet().contains("data/room")){
				Room room = item.getMaterialRoomByRoomId();
				gqlRoom = RoomMapper.MAPPER.toRoom(room);
			}

			GQLDepartment gqlDepartment = null;
			if(env.getSelectionSet().contains("data/department")){

				OrgDepartments  orgDepartments = item.getSystemctlOrgDepartmentsByDepartmentId();
				gqlDepartment = DepartmentMapper.MAPPER.toOrgDepartments(orgDepartments);

			}

			GQLTeacher gqlTeacher = null;
			if(env.getSelectionSet().contains("data/headUser")){

				TInformation teacher = item.getTeacherTInformationByHeadUser();
				gqlTeacher = TeacherMapper.MAPPER.toTeacher(teacher);
			}

			GQLTeacherOffice teacherOffice = TeacherOfficeMapper.MAPPER.mapEntity(item);
			teacherOffice.setBuildings(gqlbuildings);
			teacherOffice.setUnit(gqlUnit);
			teacherOffice.setFloor(gqlFloor);
			teacherOffice.setRoom(gqlRoom);
			teacherOffice.setDepartment(gqlDepartment);
			teacherOffice.setHeadUser(gqlTeacher);
			GQLTeacherOffice.add(teacherOffice);

		}
//		List<GQLTeacherOffice> mapEntityList = TeacherOfficeMapper.MAPPER.mapEntityList(list);

		Long total = 0L;
		if (env.getSelectionSet().contains("total")){
			total = officeArrangeDao.getTotalCount(hql);
		}
		return Result.ok().put("data", GQLTeacherOffice).put("total", total);
	}
}
